<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="填表人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposer">
              <select-user-by-dep :disabled=formDisabled  :multi="false"
                                  v-model="model.proposer" disabled></select-user-by-dep>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="填写时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date :disabled=formDisabled placeholder="请输入填写时间" v-model="model.createTime"  :show-time="true"
                     date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposerDeptId">
              <j-select-depart :disabled="formDisabled" v-model="model.proposerDeptId" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="主负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainLeader">
              <select-user-by-dep :disabled=formDisabled  :multi="false"
                                  v-model="model.mainLeader"  ></select-user-by-dep>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="团队别名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="aliasName">
              <a-input :disabled=formDisabled v-model="model.aliasName" placeholder="请输入团队别名" :max-length="255" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关联团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
              <select-team-input :multi="true" :disabled="formDisabled" v-model="model.teamId"   />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled="formDisabled" v-model="model.remark" rows="4" placeholder="请输入备注" :max-length="500"  />
            </a-form-model-item>
          </a-col>
        </a-row>
        </a-collapse-panel>
        <a-collapse-panel key="2" header="附件信息">
          <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="filePath">
              <j-upload :disabled=formDisabled v-model="model.filePath" ></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-collapse-panel>
      </a-collapse>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import CommonFlowList from '@/views/components/CommonFlowList.vue';
  import moment from 'moment';
  import { mapGetters } from 'vuex';
  export default {
    name: 'ErpFinanceTeamManagerForm',
    components: {
      SelectUserByDep,
      SelectPersonInput,
      SelectTeamInput,
      SelectCustomerInput,
      SelectProjectInput,
      SelectCompanyInput,
      CommonFlowList
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        activeKey: ['1', '2'],
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          mainLeader: [
            { required: true, message: '请选择主负责人!' }
          ],
          aliasName: [
            { required: true, message: '请输入团队别名!' }
          ],
          teamId: [
            { required: true, message: '请选择关联团队!' }
          ],
        },
        url: {
          add: "/api/erp/finance/team/erpFinanceTeamManager/add",
          edit: "/api/erp/finance/team/erpFinanceTeamManager/edit",
          queryById: "/api/erp/finance/team/erpFinanceTeamManager/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
        this.model.proposer = this.userInfo().empId; // 当前登录人
        this.model.proposerDeptId = this.userInfo().deptId; // 当前登录人部门
        this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss'); // 申请时间
      },
      edit (record) {
        this.$refs.form.resetFields();
        if (record.id) {
          getAction(this.url.queryById, { 'id': record.id }).then((res) => {
            if (res.success) {
              this.model = res.result;
            }
          })
        } else {
          this.model = Object.assign({}, record);
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
                that.$emit('unButtonDisable');
              }).finally(() => {
              that.confirmLoading = false;
            })
          }else {
            that.$emit('unButtonDisable');
          }
        })
      },
    }
  }
</script>
